#!/usr/bin/perl -w
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Small test to setup a backup with $nb_files*2 to backup,
# then, we can run multiple full jobs over the directory
# and create a "large" catalog.
#
use strict;

use lib '.';
# use bacula functions
use scripts::functions;

start_test();

# cleanup the previous conf
cleanup();

# initialize the configuration
system("scripts//copy-test-confs");

# Force to have a prompt with two different SDs
add_attribute("$conf/bacula-dir.conf", "SDPort", "1234", "Storage", "File2");

# initialize the fileset
add_to_backup_list("$cwd/build/po");

start_bacula();

create_bconcmds(
    "$out $tmp/log1.out",
    "label volume=TestVolume001 storage=File pool=Scratch",
    "run job=NightlySave level=full yes",
    "wait",
    "messages",
    "$out $tmp/log2.out",
    "llist jobid=1",
    );

run_bconsole();

my $job = `awk '/[jJ]ob: / { print \$2 }' $tmp/log2.out`;
chomp($job);

p("Now try to cancel the previous job to see if the director will connect to FD/SD");

create_bconcmds(
    "$out $tmp/log3.out",
    "cancel inactive client=$CLIENT ujobid=$job",
    "1",                        # Storage selection
    "$out $tmp/log4.out",
    "cancel inactive ujobid=$job",
    "1",
    "$out $tmp/log5.out",
    "cancel inactive storage=File ujobid=$job",
    "$out $tmp/log6.out",
    "cancel inactive jobid=1",
    "."
    );

run_bconsole();

stop_bacula();

p("Should find FD code 2901 and SD 3904 in cancel output");

if (!docmd("grep 2901 $tmp/log3.out") ||
    !docmd("grep 2901 $tmp/log4.out") ||
    !docmd("grep 2901 $tmp/log5.out") ||
    !docmd("grep 2901 $tmp/log6.out") ||
    !docmd("grep 3904 $tmp/log3.out") ||
    !docmd("grep 3904 $tmp/log4.out") ||
    !docmd("grep 3904 $tmp/log5.out") ||
    docmd("grep 3904 $tmp/log6.out")) 
{
    print "ERROR: Unable to find 2901 or 3904 in log3,4,5,6.out\n";
    $estat=1;
}

end_test();
